package fileoperation;

import copy.Copy;

import java.io.*;
import java.util.Scanner;

public class FileCopy {
    //copy source to res
    public static void copy(File source, File des) throws Exception{
        try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(des))){
            int r, numRead = 0;
            while((r=in.read())!=-1){
                out.write((byte)r);
                numRead++;
            }
            System.out.println(numRead + " bytes copied");
        }
    }

    //此函数更改自FileCreation.recoverWorkTree(),用于反序列化文件夹
    public static void copyTree(File tree, File des) throws Exception{
        try(Scanner in = new Scanner(tree)){
            while(in.hasNext()){
                String line = in.nextLine();
                String[] lineSplited = line.split(" ");
                String type = lineSplited[1];
                String hashcode = lineSplited[2];
                String fileName = lineSplited[3];
                if(type.equals("blob")){
                    Copy.copy(new File(tree.getParent(),hashcode),new File(des,fileName));
                }
                else if(type.equals("tree")){
                    File newDes = new File(des,fileName);
                    copyTree(new File(tree.getParent(), hashcode),newDes);
                    //.getParent()函数，以字符串形式返回文件所在的文件路径。
                    //(tree.getParent(), hashcode)为树节点的文件名
                }
            }
        }
    }
}
